package com.iris.live.services.report.sources;

import com.iris.live.services.common.NumberUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.SalesRepository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.models.RequestBase;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: luojie
 * date: 2017/9/6 18:07
 */
@Component("ExportInventorySource")
public class ExportInventorySource implements IExcelReportSource {
    @Autowired
    private SalesRepository repository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("经销商 销售行政标准导出模板", "经销商 销售行政标准导出模板", true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) {
        String dealer = (String) context.get("dealer");
        Map params = (Map)context.get("param");
        String[] columns = (String[]) context.get("columns"), columnPair;
        //keys存放数据Key， titles存放列表头
        String[] keys = new String[columns.length], titles = new String[columns.length + 1];

        int index;
        List<Row> rows = new ArrayList<>();
        for(index = 0; index < 5; index++) {
            rows.add(new Row());
        }

        //标题输出
        titles[0] = "NO.";
        for(index = 0; index < columns.length; index++){
            columnPair = columns[index].split(",");
            keys[index] = columnPair[1];
            titles[index+1] = columnPair[0];
        }
        rows.add(new Row(getTitleCells(titles)));

        //数据输出
        index = 0;
        RequestBase request = (RequestBase) context.get("request");
        PageData pageData = repository.getInventoryInfo(dealer,params, request);
        if(pageData != null) {
            List datas = pageData.getData();
            if (datas != null) {
                for (Object o : datas) {
                    rows.add(new Row(getDataCells(index++, keys, o)));
                }
            }
        }

        return rows;
    }

    /**
     * 获取Excel 标题行
     * @param titles
     * @return
     */
    private  Cell[] getTitleCells(String[] titles){
        Cell[] cells = new Cell[titles.length];
        for(int index = 0; index < titles.length; index++){
            cells[index] = new Cell(titles[index], index );
        }

        return cells;
    }

    /**
     * 获取数据行
     * @param rowIndex
     * @param columns
     * @param o
     * @return
     */
    private Cell[] getDataCells(int rowIndex, String[] columns, Object o) {
        Map map = (Map) o;
        Cell[] cells = new Cell[columns.length + 1];
        cells[0] = new Cell(rowIndex + "", 0);
        Object value = null;
        for(int index = 0; index < columns.length; index++){
            value = map.get(columns[index]);
            //获取数据类型，用以作为写入excel时的数据类型（修改原因：数据写入excel时，生产号以科学计数法的形式显示）
            String type = null;
            if (!StringUtils.isNullOrBlank(map.get(columns[index]))) {
                type = map.get(columns[index]).getClass().getSimpleName();
            }

            if(StringUtils.isNullOrBlank(value)){
                cells[index + 1] = new Cell("", index + 1);
            }else if(NumberUtils.isDouble(map.get(columns[index])) && ("Double".equals(type) || "Decimal".equals(type))){ //Double型小数点后保留两位, Added by Tony, 2017-06-27
                cells[index + 1] = new Cell(NumberUtils.format(Double.valueOf(map.get(columns[index]).toString()), 2), index + 1);
            }else {
                cells[index + 1] = new Cell(map.get(columns[index]) == null ? "" : map.get(columns[index]).toString(), index + 1);
            }
        }

        return cells;
    }
}